/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Windows.Input;
using System.Linq;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    public static class NTBindings
    {
        private static int tableNumber = 0;

        private static bool isBarSeatMode = false;
        public static bool IsBarSeatMode
        {
            get { return isBarSeatMode; }
            set { isBarSeatMode = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public static void Init()
        {
            NewTableWindow.Keypad.SetOkKeyCallbackDel(new KeyPad.OkCallbackDelType(OkKeyPressed_NOTHROW));
            NewTableWindow.Keypad.SetNumericKeyCallbackDel(new KeyPad.NumericKeyCallbackDelType(NumericKeyPressed_NOTHROW));
            NewTableWindow.Keypad.SetClearKeyCallbackDel(new KeyPad.ClearCallbackDelType(ClearKeyPressed_NOTHROW));
            NewTableWindow.ExitButton.MouseDown += new MouseButtonEventHandler(cancelButton_MouseDown_NOTHROW);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param Name="sender"></param>
        /// <param Name="e"></param>
        static void cancelButton_MouseDown_NOTHROW(object sender, MouseButtonEventArgs e)
        {
            try
            {
                e.Handled = true;
                Oceanside.ControlPanelWindow.Show();
                ClearTableNumber();
            }
            catch (Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_NTBIND_CANCEL_BUTTON_MOUSE_DOWN, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private static void OkKeyPressed_NOTHROW()
        {
            try
            {
                if (tableNumber != 0)
                {
                    //Make sure the table is not already in use.
                    if (SHIFT.Current.CheckTableAvailibility(
                        tableNumber,
                        isBarSeatMode))
                    {
                        if (isBarSeatMode)
                        {
                            POBindings.TableDisplayString = "Bar #" + tableNumber;
                        }

                        else
                        {
                            POBindings.TableDisplayString = "Table #" + tableNumber;
                        }

                        KIOSK kiosk = KIOSK.Get(SHIFT.GlobalContext);
                        int howManyPeople = 1;

                        if (kiosk.CreateAutoSeats)
                        {
                            HowManyBox hmb = new HowManyBox(true);
                            hmb.ShowDialog();
                            howManyPeople = HowManyBox.LastNumberSelection;
                        }

                        ///Add a new table
                        if (howManyPeople != 0)
                        {
                            SHIFT.Current.AddTable(
                                tableNumber, 
                                SHIFT.Current.User,
                                isBarSeatMode, 
                                howManyPeople);

                            ///Switch to the order window
                            Oceanside.OrderApi.Show();
                        }
                        ///Clear the table label string so it is empty the next time back to this page.
                        ClearTableNumber();
                    }
                    else
                    {
                        string message = "";

                        if (isBarSeatMode)
                        {
                            message = "Bar #" + tableNumber + " is already in use.";
                        }
                        else
                        {
                            message = "Table #" + tableNumber + " is already in use.";
                        }

                        UMD.ShowGuiMessageSTASafe_NOTHROW(message);
                    }
                }
            }
            catch (Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_NTBIND_OK_KEY_PRESSED, exc);
            }
        }

        public static void HowManyCallBack_NOTHROW(int numPeople)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        public static void PrepareForScreenChange_NOTHROW()
        {
            try
            {
                ClearTableNumber();
                NewTableWindow.AssignDataContext(SHIFT.Current);
                SHIFT.Current.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("UserDisplayName"));
            }
            catch (Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_NTBIND_PREPARE_FOR_SCREEN_CHANGE, exc);
            }

        }

        /// <summary>
        /// 
        /// </summary>
        private static void ClearTableNumber()
        {
            tableNumber = 0;

            if (isBarSeatMode)
            {
                NewTableWindow.LabelTableNumber.TableLabelString = "Bar # ";
            }
            else
            {
                NewTableWindow.LabelTableNumber.TableLabelString = "Table # ";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="val"></param>
        private static void NumericKeyPressed_NOTHROW(int val)
        {
            try
            {
                ///See if the table numTimes has exceeded 3 digits already as 4 is the max
                if (tableNumber < 1000)
                {
                    if (!(tableNumber == 0 && val == 0))
                    {
                        ///Shift the numTimes to the left and add the new obj in.
                        tableNumber = tableNumber * 10 + val;

                        if (isBarSeatMode)
                        {
                            NewTableWindow.LabelTableNumber.TableLabelString = "Bar # " + tableNumber;
                        }
                        else
                        {
                            NewTableWindow.LabelTableNumber.TableLabelString = "Table # " + tableNumber;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_NTBIND_NUMERIC_KEY_PRESSED, exc);
            }
        }

        private static void ClearKeyPressed_NOTHROW()
        {
            try
            {
                ClearTableNumber();
            }
            catch (Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_NTBIND_CLEAR_PRESSED, exc);
            }
        }
    }
}
